package com.tebiecloud.mpweixin.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUser;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserExpand;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserWallet;
import com.tebiecloud.mpweixin.client.model.vo.DeductUserAmount;
import com.tebiecloud.mpweixin.client.model.vo.PlusUserAmountVo;
import com.tebiecloud.mpweixin.client.service.IMpweixinUserServiceClient;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserWalletMapper;
import com.tebiecloud.mpweixin.server.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/feign/mpweixinuser")
public class UserExpandController implements IMpweixinUserServiceClient {

    @Autowired
    MpweixinUserExpandService mpweixinUserExpandService;

    @Autowired
    MpweixinUserService mpweixinUserService;

    @Autowired
    MpweixinUserWalletService mpweixinUserWalletService;

    @Autowired
    MpweixinUserFlowService mpweixinUserFlowService;

    @Autowired
    MpweixinMggFlowService mpweixinMggFlowService;

    @PostMapping("selectUserInfo")
    @Override
    public ResultBody<MpweixinUserExpand> selectUserInfo(@RequestParam("userId") long userId,
                                                         @RequestParam(value = "openid",required = false) String openid) {
        MpweixinUserExpand mpweixinUserExpand = mpweixinUserExpandService.createOrFind(userId,openid);
        if(mpweixinUserExpand != null) {
            return ResultBodyUtils.ok(mpweixinUserExpand);
        }
        return ResultBodyUtils.failed();
    }

    @PostMapping("selectUserWallet")
    @Override
    public ResultBody<MpweixinUserWallet> selectUserWallet(long userId) {
        MpweixinUserWallet mpweixinUserWallet = mpweixinUserWalletService.createOrFind(userId);
        return ResultBodyUtils.ok(mpweixinUserWallet);
    }

    @PostMapping("updateDeductAmount")
    @Override
    public ResultBody<DeductUserAmount> updateDeductAmount(@RequestParam("userId") long userId, @RequestParam("amount") int amount, @RequestParam("maxCoin") int maxCoin) {
        DeductUserAmount deductAmountVo = mpweixinUserWalletService.updateDeductAmount(userId,amount,maxCoin);
        return ResultBodyUtils.ok(deductAmountVo);
    }

    @PostMapping("updatePlusAmount")
    @Override
    public ResultBody<PlusUserAmountVo> updatePlusAmount(@RequestParam("userId") long userId, @RequestParam("amount") int amount, @RequestParam("coin") int coin) {
        PlusUserAmountVo plusUserAmountVo = mpweixinUserWalletService.updatePlusAmount(userId,amount,coin);
        return ResultBodyUtils.ok(plusUserAmountVo);
    }

    @PostMapping("selectMpUserInfo")
    @Override
    public ResultBody<MpweixinUser> selectMpUserInfo(@RequestParam("openid") String openid) {
        QueryWrapper<MpweixinUser> mpweixinUserQueryWrapper = new QueryWrapper<>();
        mpweixinUserQueryWrapper.eq("openid",openid);
        return ResultBodyUtils.ok( mpweixinUserService.getOne(mpweixinUserQueryWrapper));
    }

    @PostMapping("callBackPlusAmount")
    @Override
    public ResultBody<PlusUserAmountVo> callBackPlusAmount(@RequestParam("orderId") long orderId,@RequestParam("userId") long userId,
                                                           @RequestParam("amount") int amount, @RequestParam("coin") int coin, @RequestParam("type") int type) {
        PlusUserAmountVo plusUserAmountVo = mpweixinUserWalletService.callBackPlusAmount(orderId,userId,amount,coin,type);
        return ResultBodyUtils.ok(plusUserAmountVo);
    }

    @PostMapping("saveFlow")
    @Override
    public ResultBody saveFlow(@RequestParam("orderId") long orderId,@RequestParam("userId") long userId,
                                                 @RequestParam("amount") int amount,@RequestParam("type") int type) {
        mpweixinUserFlowService.saveFlow(userId,orderId,amount,type);
        return ResultBodyUtils.ok();
    }

    @PostMapping("saveCoinFlow")
    @Override
    public ResultBody saveCoinFlow(long orderId, long userId, int amount, int type) {
        mpweixinMggFlowService.saveCoinFlow(userId,orderId,amount,type);
        return ResultBodyUtils.ok();
    }
}
